<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\CreateUserRequest;
use App\Models\User;
use Doctrine\DBAL\DBALException;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Inputs;
use Mockery\CountValidator\Exception;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Facades\JWTAuth;

class UserController extends Controller
{
    /**
     * @param Request $request
     * @return bool|string
     */
    public function login(Request $request){
//        $user = User::where('mobile',$request->mobile)->first();
//        if(!$user)
//        {
//            return \Response::json(['The user does not exist'],401);
//        }
//        if($user->mobile == bcrypt($request->password) ){
//            return ['mobile'=>$user->mobile,'token'=>JWTAuth::fromUser($user)];
//        }else{
//            return json_encode(['password_error'],500);
//        }
        $credentials = $request->only('mobile', 'password');
        try {
            if (! $token = JWTAuth::attempt($credentials)) {
                return \Response::json(['error' => '账号或密码不正确'], 401);
            }
            return ["user"=>JWTAuth::toUser($token),"token"=>$token];
        } catch (JWTException $e) {
            return \Response::json(['error' => 'could_not_create_token'], 500);
        }

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateUserRequest $request)
    {

        $user = new User();
        $user->mobile = $request->input('mobile');
        $user->password = $request->input('password');

        try{
            if($user->save()){
                return ['mobile'=>$user->mobile,'token'=>JWTAuth::fromUser($user)];
            }else{
                return \Response::json(['register error'],500);
            }
        }catch(DBALException $e){
            return $e->getMessage();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
